import { useState, useCallback, useRef, useEffect } from 'react';
import { Link, useSearchParams } from 'react-router-dom';
import {
  Card,
  Breadcrumb,
  Form,
  Button,
  Radio,
  Input,
  Upload,
  Space,
  Select,
  message
} from 'antd';
import { PlusOutlined } from '@ant-design/icons';
import ReactQuill from 'react-quill';
import { addArticle, getArticleDetail, updateArticle } from '@/apis/publish';
import useChannels from '@/hooks/useChannels';

import 'react-quill/dist/quill.snow.css';
import './index.scss';


const { Option } = Select;


const Publish = () => {
  // 回填数据
  const [searchParams] = useSearchParams();
  const articleId = searchParams.get('id');

  const [form] = Form.useForm();


  // 频道列表
  const { channelList } = useChannels();

  // 上传图片
  const cacheImageList = useRef([]);
  // 控制图片Type
  const [imageType, setImageType] = useState(1)
  // 上传图片
  const [imageList, setImageList] = useState([]);

  const handleTypeChange = useCallback(
    e => {
      const type = e.target.value;
      setImageType(type);
      if (type === 1) {
        // 单图，截取第一张展示
        const imgList = cacheImageList.current[0] ? [cacheImageList.current[0]] : [];
        setImageList(imgList)
      } else if (type === 3) {
        // 三图，取所有图片展示
        setImageList(cacheImageList.current);
      }
    },
    []
  );


  const handleUploadChange = useCallback(
    info => {
      setImageList(info.fileList);
      cacheImageList.current = info.fileList;
    },
    []
  );

  // 发布文章
  const handleFinish = useCallback(
    async (formValue) => {
      const { channel_id, content, title } = formValue;
      const formatUrl = (list) => {
        return list.map(item => {
          if (item.response) {
            return item.response.data.url
          }
          else {
            return item.url
          }
        })
      }
      const params = {
        title,
        channel_id,
        type: imageType,
        cover: {
          type: imageType,
          images: formatUrl(imageList)
        },
        content
      }
      if (imageType !== imageList.length) return message.warning('图片类型和数量不一致');
      if (articleId) {
        // 编辑
        await updateArticle({ articleId, ...params });
      }
      else {
        // 新增
        await addArticle(params);
      }

      
      message.success(`${articleId ? '编辑' : '发布'}文章成功`);
    },
    [imageList, imageType, articleId]
  );

  useEffect(
    () => {
      async function getArticle () {
        const { cover, ...formValue } = await getArticleDetail(articleId);
        // 1. 回填表单数据
        form.setFieldsValue({ ...formValue, type: cover.type });
        // 2. 回填封面图片
        setImageType(cover.type) // 封面类型
        setImageList(cover.images.map(url => ({ url }))) // 封面list
      }
      if (articleId) {
        // 拉取数据回显
        getArticle();
      }
    },
    [articleId, form]
  );

  return (
    <div className="publish">
      <Card
        title={
          <Breadcrumb items={[
            { title: <Link to={'/'}>首页</Link> },
            { title: `${articleId ? '编辑文章' : '发布文章'}` },
          ]}
          />
        }
      >
        <Form
          form={form}
          labelCol={{ span: 4 }}
          wrapperCol={{ span: 16 }}
          initialValues={{ type: 1 }}
          onFinish={handleFinish}
        >

          <Form.Item
            label="标题"
            name="title"
            rules={[{ required: true, message: '请输入文章标题' }]}
          >
            <Input placeholder="请输入文章标题" style={{ width: 400 }} />
          </Form.Item>

          <Form.Item
            label="频道"
            name="channel_id"
            rules={[{ required: true, message: '请选择文章频道' }]}
          >
            <Select placeholder="请选择文章频道" style={{ width: 200 }}>
              {
                channelList.map(item => {
                  return (
                    <Option key={item.id} value={item.id}>
                      {item.name}
                    </Option>
                  )
                })
              }
            </Select>
          </Form.Item>

          <Form.Item label="封面">
            <Form.Item name="type">
              <Radio.Group onChange={handleTypeChange}>
                <Radio value={1}>单图</Radio>
                <Radio value={3}>三图</Radio>
                <Radio value={0}>无图</Radio>
              </Radio.Group>
            </Form.Item>
            {
              imageType > 0 && (
                <Upload
                  name="image"
                  listType="picture-card"
                  className="avatar-uploader"
                  showUploadList
                  action={'http://geek.itheima.net/v1_0/upload'}
                  onChange={handleUploadChange}
                  maxCount={imageType}
                  multiple={imageType > 1}
                  fileList={imageList}
                >
                  <div style={{ marginTop: 8 }}>
                    <PlusOutlined />
                  </div>
                </Upload>
              )
            }
          </Form.Item>
          
          <Form.Item
            label="内容"
            name="content"
            rules={[{ required: true, message: '请输入文章内容' }]}
          >
            <ReactQuill
              className="publish-quill"
              theme="snow"
              placeholder="请输入文章内容"
            />
          </Form.Item>

          <Form.Item wrapperCol={{ offset: 4 }}>
            <Space>
              <Button size="large" type="primary" htmlType="submit">
                发布文章
              </Button>
            </Space>
          </Form.Item>
        </Form>
      </Card>
    </div>
  )
}
export default Publish